package com.carhome.ui.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;

public class BaseFragmentActivity extends FragmentActivity {

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 打印当前activity名称
		Log.d("BaseActivity", getClass().getSimpleName());
		FragmentActivityCollector.addActivity(this);
	}

	protected void onDestroy() {
		super.onDestroy();
		FragmentActivityCollector.removeActivity(this);
	}

	/**
	 * 通过类名启动Activity
	 * 
	 * @param classActivity
	 *            要启动的Activity类
	 */
	protected void openActivity(Class<?> classActivity) {
		openActivity(classActivity, null);
	}

	/**
	 * 通过类名启动Activity，并传递参数
	 * 
	 * @param classActivity
	 * @param bundle
	 */
	protected void openActivity(Class<?> classActivity, Bundle bundle) {
		Intent intent = new Intent(this, classActivity);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
	}

	/**
	 * 通过activity启动activity
	 * 
	 * @param action
	 */
	protected void openActivity(String action) {
		openActivity(action, null);
	}

	/**
	 * 通过action启动activity，并传递参数
	 * 
	 * @param action
	 * @param bundle
	 */
	protected void openActivity(String action, Bundle bundle) {
		Intent intent = new Intent(action);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
	}

	/**
	 * 打开fragment
	 * 
	 * @param fm
	 *            传入的FragmentManager参数
	 * @param id
	 *            要装载的控件id
	 * @param classFragment
	 *            Fragment类名
	 */
	protected void openFragment(FragmentManager fm, int id, Class<?> classFragment) {
		openFragment(fm, id, classFragment, null);
	}

	/**
	 * 打开fragment 并设置tag
	 * 
	 * @param fm
	 *            传入的FragmentManager参数
	 * @param id
	 *            要装载的控件id
	 * @param classFragment
	 *            Fragment类名
	 * @param tag
	 *            为fragment设置tag
	 */
	protected void openFragment(FragmentManager fm, int id, Class<?> classFragment, String tag) {
		try {
			Fragment fragment = (Fragment) classFragment.newInstance();
			if (tag == null) {
				fm.beginTransaction().replace(id, fragment).commit();
			} else {
				fm.beginTransaction().replace(id, fragment, tag).commit();
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
}
